package cn.doitedu.json;

import com.alibaba.fastjson.JSON;
import lombok.*;

import java.util.HashMap;
import java.util.List;

public class JsonParseDemo2 {


    public static void main(String[] args) {

        String json = "{\n" +
                "  \"order_id\": \"order001\",\n" +
                "  \"order_amount\": 1200.0,\n" +
                "  \"member_id\": 100,\n" +
                "  \"address\": \"上海市闵行区联航路1888号上计信息楼A座\",\n" +
                "  \"items\": [\n" +
                "    {\n" +
                "      \"item_id\": \"item001\",\n" +
                "      \"item_price\": 18.8,\n" +
                "      \"quantity\": 10,\n" +
                "      \"style\": {\n" +
                "        \"size\": \"XXL\",\n" +
                "        \"color\": \"RED\"\n" +
                "      }\n" +
                "    },\n" +
                "    {\n" +
                "      \"item_id\": \"item003\",\n" +
                "      \"item_price\": 38.2,\n" +
                "      \"quantity\": 20,\n" +
                "      \"style\": {\n" +
                "        \"size\": \"XL\",\n" +
                "        \"color\": \"GREEN\"\n" +
                "      }\n" +
                "    }\n" +
                "  ]\n" +
                "}";


        Order order = JSON.parseObject(json, Order.class);

        List<Item> items = order.getItems();
        for (Item item : items) {
            System.out.println(item);
        }


        // 把对象变成json串
        String jsonString = JSON.toJSONString(order);
        System.out.println(jsonString);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("a",100);
        map.put("b",200);
        map.put("c",300);
        System.out.println(JSON.toJSONString(map));


    }



    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class Order{

        String order_id;
        double order_amount;
        int member_id;
        String address;
        List<Item> items;

    }

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class Item{

        String item_id;
        double item_price;
        int quantity;
        Style style;

    }

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class Style{
        String size;
        String color;
    }

}
